"""author: wenyao
   data: 2020/10/22
   project: ludingji
"""
# 什么是字典( dict )
# • 字典是Python中一个键值映射的数据结构
# • 字典是无序的
# • 字典是可变对象
# • key必须是唯一的, so 天生去重

#字典的定义
# >>> a = {}
# >>> b = dict()
# >>> type(a)
# <class 'dict'>
# >>> type(b)
# <class 'dict'>
# >>> c = [('root','123456'),('admin','admin')]
# >>> d = dict(c)
# >>> d
# {'root': '123456', 'admin': 'admin'}

##字典是无序的
#python3.6之后 字典的存储做了优化
# [root@mysql-binary ~]# python3
# Python 3.6.8 (default, Apr  2 2020, 13:34:55)
# [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
# Type "help", "copyright", "credits" or "license" for more information.
# >>> a = {'a': 1, 'b': 2, 'c': 3}
# >>> a
# {'a': 1, 'b': 2, 'c': 3}
# ───────────────────────────────────────────────────────────────────────────
# [root@mysql-binary ~]# python2
# Python 2.7.5 (default, Apr  2 2020, 13:16:51)
# [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
# Type "help", "copyright", "credits" or "license" for more information.
# >>> a = {'a': 1, 'b': 2, 'c': 3}
# >>> a
# {'a': 1, 'c': 3, 'b': 2}


############
# 字典的定义
# • 字典的key必须是可hash对象 （不可变数据类型）
# • python中所有字符串、数字、元组、布尔值、None都是可hash对象
# • 字典的value可以是任何值
#
# >>> a = {True:2}
# >>> a
# {True: 2}
# >>> a = {True:2,"ab":2}
# >>> a = {True:2,"ab":2,3:4}
# >>> a
# {True: 2, 'ab': 2, 3: 4}
# >>> a = {True:2,"ab":2,3:4,[1,2]:5}
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unhashable type: 'list'

####重复定义key
# >>> a = {"a":1,"b":2,"a":3}
# >>> a
# {'a': 3, 'b': 2}
# >>> a = {"a":1,"b":2,"a":3,"a":4}
# >>> a
# {'a': 4, 'b': 2}


##########新增、修改、取值############
#取值
# 一：dict[key]
# >>> a['c']
# 4
# >>> a['d']   #当key不存在时会报错。
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# KeyError: 'd'

#二：dict.get(key,default)  获取key的值，获取不到就返回默认default的值
# ... a
# {'a': 4, 'b': 2, 'c': 4}
# >>> a.get('d')
# >>> c = a.get('d')
# >>> c
# >>> print(c)
# None
# >>> c = a.get('d',0)
# >>> c
# 0
# >>> c = a.get('a',0)
# >>> c
# 4

##########新增修改
# 用法1：dicts[key] = value
# • 当key不存在时，表示增加一个键值对
# • 当key存在时，表示修改对应的value值
# >>> a
# {'a': 4, 'b': 2, 'c': 4}
# >>> a['a'] = 5
# >>> a
# {'a': 5, 'b': 2, 'c': 4}
# >>> a['d'] = False
# >>> a
# {'a': 5, 'b': 2, 'c': 4, 'd': False}

#字典基本操作-删除
# • 用法1：dicts.pop(key)
# • 用法2：dicts.popitem()

# 合并
# 用法1：dict1.update(dict2) => 将dict2合并到dict1
# 用法2：dict(dict1, **dict2) => 将dict1与dict2合并生成一个新字典

#unpack  字典--->{"a":1,"b":2} ---> a=1,b=2  一般作为形参
# a = {"name":"wen","age":18}
#print("my name is {name}, my age is {age}".format(name='wen',age=18))
#print("my name is {name}, my age is {age}".format(**a))

# def dict_test(x,y):
#     print(f"x is {x}, y is {y}")
#
# a = {"x":"xxxxx","y":"yyyyyyy"}
# dict_test(x="123",y="456")
# dict_test(x = a['x'],y=a['y'])
# dict_test(**a)
#
# b = [3,4]
# c = ('a','b')
# dict_test(1,2)
# dict_test(*b)
# dict_test(*c)

####成员关系
# 字典基本操作-成员关系
# • 用法：
# • item in dicts
# • item not in dicts
# {'x': 1, 'y': 2, 'z': 3}
# >>> 'x' in a
# True
# >>> 2 in a
# False
# >>> 2 in a.values()
# True
# >>> a.values()
# dict_values([1, 2, 3])

############遍历
# >>> for i in a.items():
# ...     print("key is {key}, value is {value}")
# ...
# key is {key}, value is {value}
# key is {key}, value is {value}
# key is {key}, value is {value}
# >>> for i,j in a.items():
# ...    print(f"key is {i}, value is {j}")
# ... key is x, value is 1
# key is y, value is 2
# key is z, value is 3
# >>> for i in a.items():
# ...    print(f"key is {i[0]}, value is {i[1]}")
# ...
# key is x, value is 1
# key is y, value is 2
# key is z, value is 3


# company={'BOSS':'唐僧','CTO':'悟空','CFO':'八戒','搬砖':'沙僧','苦力':'白龙马'}
# print(company['CTO'])
# print(company['BOSS'])
# for i,j in company.items():
#     print(f"{i}是{j}")
# value1=input("请输入value: ")
# if  value1 in company.values():
#     # print(value1)
#     print(f"{value1}在company里")
#     # if value1=='悟空':
#     #     print("悟空在company里")
#     # elif value1=='白骨精':
#     #     print("白骨精在company里")
#     # else:
#     #     print("我们不判断这个")
# else:
#     print("这个东西不在company里")

# print("欢迎使用用户检测系统")
# usertest={'root':'123456','admin':'admin'}
# for i,j in usertest.items():
#     username = input('请输入用户名： ')
#     if username=='quit':
#         break
#     passwd = input('请输入密码： ')
#     if i==username and j==passwd:
#         print("验证成功")
#     else:
#         print('验证失败')

while True:
    print('欢迎使用用户检测系统')
    lst = {'root':'123456','admin':'admin'}
    username = input("请输入用户名")
    password = input("请输入密码")
    if username in lst:
        if password == lst[username]:
            print("登录成功")
        else:
            print("密码错误")
    else:
        print("用户不存在")
    str = input("请输入quit退出或者输入任意值继续")
    if str == "quit":
        break